define(function(require, exports, module){

	require('{component}popup/popup.css');
	var popup = require('{component}popup/popup'),
		mId = 'singerSongList',singerData,
		tpl = require("{view}singerSongList.tpl"),
		popupImg = require("{component}popupImg/popupImg"),
		tplPopupImg = require("{view}popupImg.tpl"),
		imglazyload = require("{component}imglazyload/imglazyload"),
	view = {

		template: _.template(tpl),

		render:function(){
			var parameter = share.getParameter(),
				tinguid = decodeURI(parameter.tinguid);
			window.scroll(0,0);
			
			if(tinguid == undefined || $('#'+mId).attr('tinguid') != tinguid){
				this.ajaxSong(tinguid);
			}else{
				share.showPage(mId);
			}
			this.reload();
		},
		reload:function(){
			var dom = $('#'+mId);
			
			dom.imglazyload();

			var play = window.controller.play.play;
			if(play){
				dom.find('.ico_music_animate').addClass('selected')
			}else{
				dom.find('.ico_music_animate').removeClass('selected')
			}
			share.tabSwitch(dom,0);
		},
		bindEvt:function(){
			var tthis = this;
			var dom = $("#"+mId);

			dom.find('.body_head').on('tap',function(){
				$(this).popupImg();
			});

			dom.find('.tab_singer img').on('tap',function(){
				$(this).popupImg();
			});

			dom.find('.back').on('tap',function(){
				var q = $('#search').attr('q');
				if(q){
					window.location.href = '#search?q='+q;
				}else{
					window.location.href = '#found';
				}
			});

			dom.find('.tabTitle li').on('tap',function(){
				share.tabSwitch(dom,$(this).index());
			});

			dom.find('.tabs .songlist').on('tap',function(){
				$(this).parent().find('li').removeClass('selected');
				$(this).addClass('selected');
				dom.find('.tabbox').hide();
				dom.find('.tab_songlist').show();
			});

			dom.find('.container_footer .tj').on('tap',function(){
				window.location.href = '#found';
			});

			dom.find('input.search').on('keydown',function(e){
				if(e.keyCode==13){
					window.location.href = '#search?q='+$(this).val();
				}
			});

			dom.find('.ico_music_animate').on('tap',function(){
				var songid = $('#play').attr('songid') || 877578;
				window.location.href = '#play?songid='+songid;
			})

			dom.find('.com_list li').on('tap',function(){
				var songid = $(this).attr('songid');
				util.setLocalStorage('play_list',singerData);
				window.location.href = '#play?songid='+songid;
			});

			dom.find('li .ico_more').on('tap',function(e){
				var songid = $(this).parents('li').attr('songid');
				e.stopPropagation();
				popup.show({type:'more',songid:songid,li:['recommended']});
			});

			dom.find('input.search').on('keydown',function(e){
				if(e.keyCode==13){
					var val = $(this).val(),newArr=[];
					var history = util.getLocalStorage('search_history');
					if(history){
						history.unshift(val);
						newArr = history;
					}else{
						newArr.unshift(val);
					}

					util.setLocalStorage('search_history',newArr)
					window.location.href = '#search?q=' + val;
				}
			});

		},
		
		ajaxSingerInfo:function(tinguid){
			var tthis = this,dom=$('#'+mId),
				domInfo = dom.find('.tab_singer');
			$.ajax(share.ajax({
				url: apiUrl,
				data:{method:'baidu.ting.artist.getInfo',tinguid:tinguid},
				success:function(data){
					singerData.singer_info = data;
					$("body").append(tthis.template(singerData));
					$('#'+mId).attr({'tinguid':tinguid});
					$('#'+mId).imglazyload();
					tthis.bindEvt();
					tthis.reload();
					share.showPage(mId);
				}
			}));
		},
		ajaxSong:function(tinguid){
			share.loading();
			var tthis = this,dom=$('#'+mId);
			$.ajax(share.ajax({
				url: apiUrl,
				data:{method:'baidu.ting.artist.getSongList',tinguid:tinguid,limits:50,use_cluster:1,order:2},
				success:function(data){
					singerData = data;
					dom.remove();
					tthis.ajaxSingerInfo(tinguid);
				}
			}));
		},
		showAfter:function(){
			var dom = $('#'+mId);
			var play = window.controller.play.play;
			if(play){
				dom.find('.ico_music_animate').addClass('selected')
			}else{
				dom.find('.ico_music_animate').removeClass('selected')
			}
		},
		
	};

	view.ajaxBool = true;
	view.userNav = false;
	module.exports =  view;
});

